package io.lckgroup.modules.lck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.lckgroup.modules.lck.dao.OrderMainDao;
import io.lckgroup.modules.lck.dao.ShopMerchantInfoDao;
import io.lckgroup.modules.lck.entity.OrderMainEntity;
import io.lckgroup.modules.lck.entity.ShopMerchantInfoEntity;
import io.lckgroup.modules.lck.service.WxgzhPayService;
import io.lckgroup.modules.lck.utils.GetPayPage;
import io.lckgroup.modules.lck.utils.MakeOrderIdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;

@Service("WxgzhPayService")
public class WxgzhPayServiceImpl implements WxgzhPayService {

    @Autowired
    private ShopMerchantInfoDao shopMerchantInfoDao;
    @Autowired
    private OrderMainDao orderMainDao;

    /**
     * 功能描述: <br>
     * 〈〉发送请求到泸州银行获取支付跳转链接
     *
     * @Param: [merchantId, orderNum]
     * @Return: java.lang.String
     * @Author: 13980
     * @Date: 2020/3/20 14:14
     */
    @Override
    public String getPayUrl(Long merchantId, String orderNum) {
        //根据id查询商家信息
        QueryWrapper<ShopMerchantInfoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", merchantId);
        ShopMerchantInfoEntity shopMerchantInfoEntity = shopMerchantInfoDao.selectOne(queryWrapper);
        //根据订单号查询主订单信息
        QueryWrapper<OrderMainEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("order_id", orderNum);
        OrderMainEntity orderMainEntity = orderMainDao.selectOne(queryWrapper1);
        //模拟数据
        String merchantNo = "99201810300937051630370";
        String storeNo = "ST201810300942563880371";
        String posNum = "TT201909240851";
        String userAgent = "WeiXIN";
        String successUrl = null;
        try {//解决前端url参数丢失
            successUrl = URLEncoder.encode("http://twwx.nat300.top/h5/pages/pay/paySuccess?orderNum=" + orderNum + "&merchantNo=" + merchantNo, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String outTradeNo = orderMainEntity.getOrderId();
        BigDecimal orderPrice = orderMainEntity.getOrderPrice();
        orderPrice = new BigDecimal("0.01");//价格测试数据
        String url = GetPayPage.getpaypage(merchantNo, storeNo, posNum, userAgent, outTradeNo, successUrl, orderPrice);
        return url;
    }

    /**
     * 重新支付（修改原来的订单号再查询出新的orderMainEntity返回）
     *
     * @param merchantId
     * @param orderNum
     * @return
     */
    @Override
    public OrderMainEntity updateOrderNum(Long merchantId, String orderNum) {
        QueryWrapper<OrderMainEntity> queryWrapper = new QueryWrapper<>();
        OrderMainEntity orderMainEntity = new OrderMainEntity();
        //将新生成的订单号放入orderMainEntity
        orderMainEntity.setOrderId(MakeOrderIdUtil.makeOrderId());
        queryWrapper.eq("order_id", orderNum);
        //将原订单号更新为新的订单号
        orderMainDao.update(orderMainEntity, queryWrapper);
        //根据新的订单号查询出orderMainEntity
        QueryWrapper<OrderMainEntity> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.setEntity(orderMainEntity);
        OrderMainEntity orderMainEntity1 = orderMainDao.selectOne(queryWrapper1);
        return orderMainEntity1;
    }
}
